package com.tz.crm.service.impl;

import com.tz.crm.dao.CustomerDao;
import com.tz.crm.dao.UserDao;
import com.tz.crm.dao.VisitRecordDao;
import com.tz.crm.dao.VisitTypeDao;
import com.tz.crm.entity.*;
import com.tz.crm.entity.dto.Report;
import com.tz.crm.entity.enumeration.Level;
import com.tz.crm.entity.enumeration.Status;
import com.tz.crm.service.VisitRecordService;
import com.tz.crm.utils.Pager;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

;

/**
 * VisitRecordServiceImpl 服务类
 * 拜访记录保存以及查询
 */
@Service
@Transactional
@SuppressWarnings("unchecked")
public class VisitRecordServiceImpl extends BaseServiceImpl<VisitRecord, String> implements VisitRecordService {

    @Autowired
    private VisitRecordDao visitRecordDao;
    @Autowired
    private VisitTypeDao visitTypeDao;
    @Autowired
    private UserDao userDao;
    @Autowired
    private CustomerDao customerDao;

    /**
     * 有参构造
     *
     * @param visitRecordDao 注入的 VisitRecordDao 组件
     */
    @Autowired
    public VisitRecordServiceImpl(VisitRecordDao visitRecordDao) {
        this.visitRecordDao = visitRecordDao;
        super.setBaseDao(visitRecordDao);
    }

    @Override
    public Pager<VisitRecord> getByPager(String customerId, Pager pager) {
        DetachedCriteria dc = DetachedCriteria.forClass(VisitRecord.class);
        DetachedCriteria customerCriteria = dc.createCriteria("customer");
        customerCriteria.add(Restrictions.eq("id", customerId));
        return visitRecordDao.findByPager(pager, dc, Order.desc("visitDate"));
    }

    @Override
    public void save(VisitRecord visitRecord, String customerId, User owner) {
        Customer customer = customerDao.get(customerId);
        VisitType vt = visitTypeDao.get(visitRecord.getVisitType().getId());
        visitRecord.setVisitType(vt);
        visitRecord.setCustomer(customer);
        visitRecord.setOwner(owner);
        if (visitRecordDao.getList("customer", customer).size() == 0) {
            visitRecord.setFirstVisit(true);
        } else {
            visitRecord.setFirstVisit(false);
        }
        visitRecordDao.save(visitRecord);
        //1. 根据customerid查询出拜访记录 数量
//        int visitCount = visitRecordDao.countNumber(customer.getId());
        //2. judge
        if (customer.getStatus() == Status.SIGNED) {
            Customer c = customerDao.get(customer.getId());
            c.setStatus(Status.SIGNED);
            //更新 customer表中的status的值，从 UNSIGN -> YICHUSHI
            customerDao.update(c);
        }
        /*else if (visitCount == 0) {
            //...
            Customer c = customerDao.get(customer.getId());
            c.setStatus(Customer.Status.UNSIGNED_YICHUSHI);
            //更新 customer表中的status的值，从 UNSIGN -> YICHUSHI
            customerDao.update(c);
        } else if (visitCount > 0) {
            Customer c = customerDao.get(customer.getId());
            c.setStatus(Customer.Status.UNSIGNED_YIFUSHI);
            customerDao.update(c);
            //...
            //更新 customer表中的status的值，从 YICHUSHI -> YIFUSHI
        }*/
    }

    @Override
    public Pager<VisitRecord> search(Boolean onlyFirstVisit, Pager pager,
                                     List<VisitType> visitTypeList, List<Level> levelList,
                                     List<User> ownerList, User user, List<Status> statusList) {

        DetachedCriteria detachedCriteria = DetachedCriteria
                .forClass(VisitRecord.class);
        if (onlyFirstVisit) {
            detachedCriteria.add(Restrictions.eq("firstVisit", true));
        }
        if (visitTypeList != null && visitTypeList.size() != 0) {
            detachedCriteria.add(Restrictions.in("visitType", visitTypeList));
        }
        if (levelList != null && levelList.size() != 0) {
            detachedCriteria.add(Restrictions.in("level", levelList));
        }
        if (statusList != null && statusList.size() != 0) {
            detachedCriteria.createCriteria("customer")
                    .add(Restrictions.in("status", statusList));
        }
        if (ownerList != null && ownerList.size() != 0) {
            detachedCriteria.add(Restrictions.in("owner", ownerList));
        } else {
            int flag = 0;
            for (Role r : user.getRoleSet()) {
                if (r.getValue().equals("admin")
                        || r.getValue().equals("salesManager")) {
                    flag = 1;
                    break;
                }
            }
            if (flag == 1) {
                ownerList = userDao.getList("dept", user.getDept());
                detachedCriteria.add(Restrictions.in("owner", ownerList));
            } else {
                user = userDao.get(user.getId());
                detachedCriteria.add(Restrictions.eq("owner", user));
            }
        }

        pager = visitRecordDao.findByPager(pager, detachedCriteria, Order.desc("visitDate"));
        return pager;
    }

    @Override
    public List<Report> report(List<User> ownerList,
                               List<VisitType> visitTypeList, Date fromDate, Date toDate, Boolean onlyWorkDays, User user) {
        return visitRecordDao.report(ownerList, visitTypeList, fromDate, toDate, onlyWorkDays, user);
    }

    /**
     * 根据客户id 删除其拜访记录
     *
     * @param id
     */
    @Override
    public void delVisitRecordByCusId(String id) {
        //根据客户id 获取所有的拜访记录
            List<VisitRecord> visitRecords=visitRecordDao.getAllByCusId(id);
            //遍历删除
        for (VisitRecord visitRecord : visitRecords) {
                visitRecordDao.delete(visitRecord);
        }

    }

    @Override
    public int countNumber(String id) {
        return visitRecordDao.getTotalCountByCustomerId(id);
    }
}
